<?php
class CdeModulesController extends AppController {
	var $name = 'CdeModules';
	var $helpers = array('Html', 'Form');
	var $uses = array('CdeModule', 'CdeMultimedia');

	// Manage module - DONE
	function manage($id = null) {
		// Check if the module ID is provided, if not display error message and redirect to index of all units
		if (!$id) {
			$this->Session->setFlash(__('Module ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}

		// Get the module by ID
		$cdeModule = $this->CdeModule->read(null, $id);
		if (!$cdeModule) {
			// If the module is not found display the error message and redirect to index of all units
			$this->Session->setFlash(__('Invalid module ID, cannot manage', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		} else {
			// Set the value to display
			$this->set('cdeModule', $cdeModule);
			$this->set('cdeReferencelinks', $this->CdeModule->CdeReferencelink->find('all', array('conditions' => array('CdeReferencelink.cde_module_id' => $cdeModule['CdeModule']['id']))));
			$this->set('cdeAnswers', $this->CdeModule->CdeAnswer->find('all', array('conditions' => array('CdeAnswer.cde_module_id' => $cdeModule['CdeModule']['id']))));
		}
	}

	// Create new module for unit - DONE
	function add($unitId = null) {
		// Check if the unit ID is provided, if not display error message and redirect to index of all units
		if (!$unitId && empty($this->data)) {
			$this->Session->setFlash(__('Unit ID is required', true));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
		
		// Get the unit detail by unit ID
		$this->set('cdeUnit', $this->CdeModule->CdeUnit->read(null, $unitId));
		// Generate options for multimedia
		$cdeMultimedias = array('' => '-- Select multimedia --');
		array_push($cdeMultimedias, $this->CdeMultimedia->find('list', array('fields' => array('CdeMultimedia.url', 'CdeMultimedia.description'))));
		$this->set('cdeMultimedias', $cdeMultimedias);
		// Generate options for question type
		$questionTypes = array('' => '-- Select question type --' ,'SINGLE_ANSWER' => 'Single answer question', 'MULTIPLE_ANSWER' => 'Multiple answer question');
		$this->set('questionTypes', $questionTypes);
		
		if (!empty($this->data)) {
			$this->CdeModule->create();
			// Check if the unit with the title is already exist
			if ($this->data['CdeModule']['title'] and $this->CdeModule->find('all', array('conditions' => array('CdeModule.title' => $this->data['CdeModule']['title'])))) {
				$this->Session->setFlash(__('Module with title "'.$this->data['CdeModule']['title'].'" is already exist, please enter again', true), 'default', array('class' => 'error-message'));
			} else {
				if ($this->CdeModule->save($this->data)) {
					// Create new module successfully, redirect back to manage page of the unit
					$this->Session->setFlash(__('The new module has been successfully created', true), 'default', array('class' => 'success-message'));
					$this->redirect(array('action'=>'../cde_units/manage/'.$this->data['CdeModule']['cde_unit_id']));
				} else {
					// Error occurred, cannot create new module
					$this->Session->setFlash(__('An error occurred, new module could not be successfully created. Please try again!', true), 'default', array('class' => 'error-message'));
				}
			}
		}
	}

	// Update module - DONE
	function edit($id = null) {
		// Check if the module ID is provided, if not display error message and redirect to index of all units
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Module ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}

		// Get the module by ID
		$cdeModule = $this->CdeModule->read(null, $id);
		if (!$cdeModule && empty($this->data)) {
			// If the module is not found display the error message and redirect to index of all units
			$this->Session->setFlash(__('Invalid module ID, cannot edit', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		} else {
			$this->set('cdeModule', $cdeModule);
			// Generate options for multimedia
			$cdeMultimedias = array('' => '-- Select multimedia --');
			array_push($cdeMultimedias, $this->CdeMultimedia->find('list', array('fields' => array('CdeMultimedia.url', 'CdeMultimedia.description'))));
			$this->set('cdeMultimedias', $cdeMultimedias);
			// Generate options for question type
			$questionTypes = array('' => '-- Select question type --' ,'SINGLE_ANSWER' => 'Single answer question', 'MULTIPLE_ANSWER' => 'Multiple answer question');
			$this->set('questionTypes', $questionTypes);

			if (!empty($this->data)) {
				// On update action
				// Check if the unit with the title is already exist
				if ($this->CdeModule->find('all', array('conditions' => array('CdeModule.title' => $this->data['CdeModule']['title'], 'CdeModule.id !=' => $this->data['CdeModule']['id'])))) {
					$this->Session->setFlash(__('Module with title "'.$this->data['CdeModule']['title'].'" is already exist, please enter again', true), 'default', array('class' => 'error-message'));
				} else {
					if ($this->CdeModule->save($this->data)) {
						// Update successfully, redirect back to manage page of the unit
						$this->Session->setFlash(__('The module has been successfully updated', true), 'default', array('class' => 'success-message'));
						$this->redirect(array('action'=>'../cde_units/manage/'.$cdeModule['CdeModule']['cde_unit_id']));
					} else {
						// Error occurred, cannot update
						$this->Session->setFlash(__('The module could not be updated. Please try again!', true), 'default', array('class' => 'error-message'));
					}
				}
			} else {
				// Set the data to update
				$this->data = $cdeModule;
			}
		}
	}
	
	// Delete module - DONE
	function delete($id = null) {
		// Check if the module ID is provided, if not display error message and redirect to index of all units
		if (!$id) {
			$this->Session->setFlash(__('Module ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
		
		// Get the module by ID
		$cdeModule = $this->CdeModule->read(null, $id);
		if ($cdeModule != null) {
			// If the module is found, delete it
			if ($this->CdeModule->del($id)) {
				// Delete successfully, redirect back to manage page of the unit
				$this->Session->setFlash(__('The module has been successfully deleted', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'../cde_units/manage/'.$cdeModule['CdeModule']['cde_unit_id']));
			} else {
				// Error occurred, cannot delete, redirect back to manage page of the unit
				$this->Session->setFlash(__('An error occurred, cannot delete the module. Please try again!', true), 'default', array('class' => 'error-message'));
				$this->redirect(array('action'=>'../cde_units/manage/'.$cdeModule['CdeModule']['cde_unit_id']));
			}
		} else {
			// If the module is not found display the error message and redirect to index of all units
			$this->Session->setFlash(__('Invalid module ID, cannot delete', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
	}

	// Disable module - DONE
	function disable($id = null) {
		// Check if the module ID is provided, if not display error message and redirect to index of all units
		if (!$id) {
			$this->Session->setFlash(__('Module ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
		
		// Get the module by ID
		$cdeModule = $this->CdeModule->read(null, $id);
		if ($cdeModule != null) {
			// If the module is found, disable it
			$this->CdeModule->id = $id;
			if ($this->CdeModule->saveField('enabled', '0', true)) {
				// Disable successfully, redirect back to manage page of the unit
				$this->Session->setFlash(__('The module has been successfully disabled', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'../cde_units/manage/'.$cdeModule['CdeModule']['cde_unit_id']));
			} else {
				// Error occurred, cannot disable, redirect back to manage page of the unit
				$this->Session->setFlash(__('An error occurred, cannot disable the module. Please try again!', true), 'default', array('class' => 'error-message'));
				$this->redirect(array('action'=>'../cde_units/manage/'.$cdeModule['CdeModule']['cde_unit_id']));
			}
		} else {
			// If the module is not found display the error message and redirect to index of all units
			$this->Session->setFlash(__('Invalid module ID, cannot disable', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
	}

	// Enable module - DONE
	function enable($id = null) {
		// Check if the module ID is provided, if not display error message and redirect to index of all units
		if (!$id) {
			$this->Session->setFlash(__('Module ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
		
		// Get the module by ID
		$cdeModule = $this->CdeModule->read(null, $id);
		if ($cdeModule != null) {
			// If the module is found, enable it
			$this->CdeModule->id = $id;
			if ($this->CdeModule->saveField('enabled', '1', true)) {
				// Enable successfully, redirect back to manage page of the unit
				$this->Session->setFlash(__('The module has been successfully enabled', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'../cde_units/manage/'.$cdeModule['CdeModule']['cde_unit_id']));
			} else {
				// Error occurred, cannot enable, redirect back to manage page of the unit
				$this->Session->setFlash(__('An error occurred, cannot enable the module. Please try again!', true), 'default', array('class' => 'error-message'));
				$this->redirect(array('action'=>'../cde_units/manage/'.$cdeModule['CdeModule']['cde_unit_id']));
			}
		} else {
			// If the module is not found display the error message and redirect to index of all units
			$this->Session->setFlash(__('Invalid module ID, cannot enable', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
	}
}
?>